package com.example.commapp.module.login;

import com.example.commapp.api.MyApiServices;
import com.example.ln.coustomapplication.base.BasePresenter;
import com.example.ln.coustomapplication.base.BaseResult;
import com.example.ln.coustomapplication.model.RetrofitHelper;
import com.example.ln.coustomapplication.model.RxSchedulers;

import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * Describe:
 * Author: Tenda
 * Time: 2018/12/10  16:23
 * Corpyright (C) 2018-2018 山东六牛科技有限公司
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter{

    @Override
    public void onLogin(String username, String password) {
        RetrofitHelper.getInstance()
                .getRetrofit()
                .create(MyApiServices.class)
                .onLogin(username,password)
                .compose(RxSchedulers.<BaseResult<List<SPUser>>>applySchedulers())
                .compose(mView.<BaseResult<List<SPUser>>>bindToLife())
                .subscribe(new Consumer<BaseResult<List<SPUser>>>() {
                    @Override
                    public void accept(BaseResult<List<SPUser>> listBaseResult) throws Exception {
                        mView.onLoginSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFailed(throwable.getMessage());
                    }
                });

    }
}
